package com.common.overwrite.log4j;

import java.io.IOException;

import org.apache.log4j.Layout;
import org.apache.log4j.helpers.QuietWriter;
/**
 * 该类主要用于log4j配置为buffer模式下，服务器停止时将最后一个buffer信息存入文件
 * 主要利用了虚拟停止的钩子来实现
 * @author 张龙
 *
 */
public class DailyRollingFileAppender extends
		org.apache.log4j.DailyRollingFileAppender {
    public DailyRollingFileAppender() {  
        super();  
        Runtime.getRuntime().addShutdownHook(new Log4jHockThread());  
    }  
    
	public DailyRollingFileAppender(Layout layout, String filename,
			String datePattern) throws IOException {
		super(layout, filename, datePattern);
		Runtime.getRuntime().addShutdownHook(new Log4jHockThread());
	}

	public QuietWriter getQw() {
		return super.qw;
	}

	private class Log4jHockThread extends Thread {
		@Override
		public void run() {
			QuietWriter qw = DailyRollingFileAppender.this.getQw();
			if (qw != null) {
				qw.flush();
			}
		}
	}
}
